package com.jt.seivice;

import com.jt.mapper.UserMapper;
import com.jt.pojo.User;
import com.jt.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.util.*;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public List<User> findAll() {
        return userMapper.findAll();
    }

    //根据u/p查询数据库
    @Override
    public String findUserByUp(User user) {
        //1.将密码加密
        byte[] bytes = user.getPassword().getBytes();
        String md5Pass = DigestUtils.md5DigestAsHex(bytes);
        System.out.println(md5Pass);
        user.setPassword(md5Pass);
        //2.根据用户名和密文查询数据库
        User userDB = userMapper.findUserByUp(user);
        //3.判断userDB是否有值
        if (userDB == null){
            return null;
        }
        //秘钥特点:  唯一性,迷惑性  UUID几乎可以保证唯一性
        String token= UUID.randomUUID().toString().replace("-", "");
        return token;
    }

    /**
     * 分页Sql:       每页10条
     *          select * from user limit  起始位置,查询条数
     * 第一页
     *          select * from user limit 0,10           含头不含尾
     * 第二页
     *          select * from user limit 10,10
     * 第三页
     *          select * from user limit 20,10
     */
    @Transactional
    @Override
    public PageResult getUserListByPage(PageResult pageResult) {
        //1.总数
        long total = userMapper.findTotal();

        //2.分页结果
        int size = pageResult.getPageSize();        //条数
        int start = (pageResult.getPageNum()-1) * size;     //起始位置
        String query = pageResult.getQuery();       //查询条件
        Map map = new HashMap();
        map.put("start", start);
        map.put("size", size);
        map.put("query", query);
        List<User> userList = userMapper.findUsetListByPage(map);
        return pageResult.setTotal(total).setRows(userList);
    }

    @Transactional
    @Override
    public void updateStatusById(User user) {
        userMapper.updateStatusById(user);
    }

    /**
     *     业务:实现业务数据封装
     *          1.密码加密
     *          2.设定默认状态
     *          3.设定当前时间
     */
    @Transactional
    @Override
    public void saveUser(User user) {
        String password = user.getPassword();
        String md5pass = DigestUtils.md5DigestAsHex(password.getBytes());

        //获取当前时间
        Date date = new Date();
        user.setPassword(md5pass).setStatus(true).setCreated(date).setUpdated(date);
        userMapper.saveUser(user);
    }

    @Override
    public User findUserById(Integer id) {
        return userMapper.findUserById(id);
    }

    @Transactional
    @Override
    public void updateUser(User user) {
        Date date = new Date();
        user.setUpdated(date);
        userMapper.updateUser(user);
    }

    /**
     * 问题:数据是否真的被删除?
     *  解决方案:@Transactional
     *  作用:
     *          1.默认添加下,只拦截运行时异常
     *          2.rollbackFor:  指定异常的类型回滚  rollbackFor=RuntimeException.class
     *          3.noRollbackFor:  指定异常不回滚   noRollbackFor=RuntimeException.class
     */
    @Transactional
    @Override
    public void deleteUser(Integer id) {
        userMapper.deleteUser(id);
    }
}
